package com.genius.adminmanager.role.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.other.entity.DataJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.role.entity.RoleJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.dingding.util.GeniusConfig;
import com.genius.dingding.util.HttpHelper;
import com.genius.workflow.ehcache.PowerUtil;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBUtils;

public class RoleService {
	
	public static void addFieldByMenuId(JSONObject json) throws ConnectionException, DBClosedException, SQLException{
		String insertField = MenuJson.menuId+","+RoleJson.roleId+","+DataJson.field;
		String menu_field_table = "t_data";
		String whereField = MenuJson.menuId+","+RoleJson.roleId;
		
		JSONObject existsJson = DBTools.getIsExist(whereField, menu_field_table, json);
		if(existsJson == null){//不存在再插入
			DBTools.insert(insertField, menu_field_table, json);
		}else{
			//存在就更新
			DBTools.update(insertField,whereField, menu_field_table, json);
		}
	}
	public static void addUserByRoleId(JSONArray arr) throws ConnectionException, DBClosedException, SQLException{
		String insertField = UserJson.userId+","+RoleJson.roleId;
		String menu_field_table = "t_user_role";
		
		String insertSql = DBTools.getInsertSql(insertField, menu_field_table);
		List<String[]> insertParms = DBTools.getInsertListParms(insertField, arr);
		
		Connection con = DBUtils.getConnect();
		DBUtils.beginTransaction(con);
		
		for(String [] parms:insertParms){
			String where = " where ";
			for(String keyWord:insertField.split(",")){
				where += keyWord+"=? and ";
			}
			where = where.substring(0, where.lastIndexOf("and"));
			
			String sql = "select 1 from "+menu_field_table+where;
			ResultSet rs = DBUtils.query(con, sql,parms);
			if(!rs.next()){
				//说明不存在
				PreparedStatement pstmt = con.prepareStatement(insertSql);
				for (int i = 0; i < parms.length; i++) {
		            if(StringUtils.equalsIgnoreCase("null", (String)parms[i]) ||StringUtils.equalsIgnoreCase("", (String)parms[i])){
		            	parms[i] = null;
			            	pstmt.setObject(i + 1, parms[i]);
			            	continue;
		            }
		            if(parms[i] != null){
		            	pstmt.setObject(i + 1, parms[i]);
		            }
		        }
				pstmt.addBatch();
				pstmt.executeBatch();
			}
			DBUtils.closeDBResources(rs);
		}
		
		DBUtils.endTransaction(con);
		DBUtils.closeDBResources(con);
	}
	public static void changeRoleSuccess(HttpSession session,JSONObject userJson,String currendRoleId){
		//存入缓存
		PowerUtil.doGetAuthorizationInfo(session,currendRoleId);
		//切换后再重新把新的角色id存日session
		session.setAttribute(SessionJson.currentUser, userJson);
		session.setAttribute(SessionJson.roleId,currendRoleId);
		//插入左侧菜单待办图标
//		getDaibanTubiao(session,userJson);
//		String a = "select count(i) from notice_send_shop where shopcode = '1004'";
	}
	//插入左侧菜单待办图标
	public static JSONObject getDaibanTubiao(HttpSession session,JSONObject userJson){
		//插入低温待办数量
		JSONObject resJson = new JSONObject();
		String url = GeniusConfig.GONGGAOLANYAOHUO+"/api/Order/OrderCount?UserNO="+userJson.getString("jobnumber");
		String resultStr = HttpHelper.httpGetString(url);
    	JSONObject resuJson = JSONObject.parseObject(resultStr);
		if(resuJson!= null && "1".equals(resuJson.getString("Status"))){
//			session.setAttribute("/unite_platform/yaohuo/toShenhe", resuJson.getJSONArray("OrderDtl").getJSONObject(0).getString("OrderCount"));
			resJson.put("/unite_platform/yaohuo/toShenhe", resuJson.getJSONArray("OrderDtl").getJSONObject(0).getString("OrderCount"));
			resJson.put("/unite_platform/zsyaohuo/toShenhe", resuJson.getJSONArray("OrderDtl").getJSONObject(0).getString("OrderCount"));
		}
//		System.out.println("resJson->"+resJson);
		return resJson;
       
	}
}
